<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>轮播图随机版本</title>
    <style>
        * {
            box-sizing: border-box;
        }

        .slider {
            width: 560px;
            height: 400px;
            overflow: hidden;
        }

        .slider-wrapper {
            width: 100%;
            height: 320px;
        }

        .slider-wrapper img {
            width: 100%;
            height: 100%;
            display: block;
        }

        .slider-footer {
            height: 80px;
            background-color: rgb(100, 67, 68);
            padding: 12px 12px 0 12px;
            position: relative;
        }

        .slider-footer .toggle {
            position: absolute;
            right: 0;
            top: 12px;
            display: flex;
        }

        .slider-footer .toggle button {
            margin-right: 12px;
            width: 28px;
            height: 28px;
            appearance: none;
            border: none;
            background: rgba(255, 255, 255, 0.1);
            color: #fff;
            border-radius: 4px;
            cursor: pointer;
        }

        .slider-footer .toggle button:hover {
            background: rgba(255, 255, 255, 0.2);
        }

        .slider-footer p {
            margin: 0;
            color: #fff;
            font-size: 18px;
            margin-bottom: 10px;
        }

        .slider-indicator {
            margin: 0;
            padding: 0;
            list-style: none;
            display: flex;
            align-items: center;
        }

        .slider-indicator li {
            width: 8px;
            height: 8px;
            margin: 4px;
            border-radius: 50%;
            background: #fff;
            opacity: 0.4;
            cursor: pointer;
        }

        .slider-indicator li.active {
            width: 12px;
            height: 12px;
            opacity: 1;
        }
    </style>
</head>

<body>
<div class="slider">
    <div class="slider-wrapper">
        <img src="img/luya_1.png" alt=""/>
    </div>
    <div class="slider-footer">
        <p>对人类来说会不会太超前了？</p>
        <ul class="slider-indicator">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
        <div class="toggle">
            <button class="prev">&lt;</button>
            <button class="next">&gt;</button>
        </div>
    </div>
</div>
<script>
    //初始化要轮播的数据，将这些数据封装到一个对象当中
    const sliderData = [
        {url: './img/luya_1.png', title: '夕阳抛竿，水花轻溅 ', color: 'rgb(100, 67, 68)'},
        {url: './img/luya_2.png', title: '溪流蹲钓，专注凝神', color: 'rgb(43, 35, 26)'},
        {url: './img/luya_3.png', title: '巨物出水，握竿发力', color: 'rgb(36, 31, 33)'},
        {url: './img/luya_4.png', title: '雨中路亚，蓑衣映光', color: 'rgb(139, 98, 66)'},
        {url: './img/luya_5.png', title: '船钓收线，风拂衣摆', color: 'rgb(67, 90, 92)'},
    ]
    //获得属性的值
    var img = document.querySelector(".slider-wrapper img");
    var title = document.querySelector(".slider-footer p");
    var color = document.querySelector(".slider-footer");

    //定义图片的数量
    var i = 0;
    //开启定时器
    setInterval(function () {
        //图片叠加进行遍历
        i++;
        //遍历完一次之后进行判断，然后再从第一次进行开始
        if (i === sliderData.length) {
            i = 0;
        }
        //更换获得的属性的地址
        img.src = sliderData[i].url;
        title.innerHTML=sliderData[i].title;
        color.style.backgroundColor=sliderData[i].color

        //更换小圆点
        var element = document.querySelector(`.slider-indicator li.active`);
        if (element){
            element.classList.remove("active")
        }
        document.querySelector(`.slider-indicator li:nth-child(${i+1})`).classList.add("active");

    },1000)


</script>
</body>
</html>